package net.onceface.web.resources;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.onceface.application.ProductApplication;
import net.onceface.application.commons.InvokeResult;
import net.onceface.application.commons.Message;

/**
 * 商品资源类
 * **/
@Path("/product")
public class ProductResource extends BaseResource{

	@Inject
	ProductApplication application;
	
	/**
	 * 按分类id搜索商品
	 * **/
	@GET
	@Path("/findByCategory")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String findByCategory(@QueryParam("categoryId")final Long categoryId, @Context HttpServletResponse response){
		if (response != null){
    		response.setHeader("Access-Control-Allow-Origin", "*");
    	}
		try{
			return toDateJson(InvokeResult.success(application.queryProductByCategory(categoryId)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_PRODUCT_FAILURE));
		}
	}
	
	/**
	 * 按商品id获取商品详情
	 * **/
	@GET
	@Path("/getProductDetails")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getProduct(@QueryParam("id")final Long id, @Context HttpServletResponse response){
		if (response != null){
    		response.setHeader("Access-Control-Allow-Origin", "*");
    	}
		try{
			return toDateJson(InvokeResult.success(application.getProductDetails(id)));
		}catch(Exception e){
			return toJson(InvokeResult.failure(Message.GET_PRODUCT_FAILURE));
		}
	}
}
